package bogdan.trainee;
import java.util.Scanner;
public class Divide {

	/**
	 * @author Olexandr Bogdan
	 * program divides the two numbers and displays a whole and a residue.
	 */
	public static void main(String[] args) {
		
		print(calculate(readConsole()));
		
	}
	
	/**
	 * 
	 * @return Complex
	 * method reads from the console and returns the numerator and denominator
	 */
	public static Complex readConsole(){
		Complex result = new Complex();
		Scanner scan = new Scanner(System.in);
		System.out.println("Enter divide :");
		result.divide = scan.nextInt();
		System.out.println("Enter divider :");
		result.divider = scan.nextInt();
		scan.close();
		return result;
	}

	/**
	 * 
	 * @param complex
	 * @return Result
	 * method divides the dividend by the divisor in a column and returns the result
	 */
	public static Result calculate(Complex complex) {
		int divide = complex.divide;
		int divider = complex.divider;
		if(divider == 0){
			System.out.print("Ділення на нуль!");
			System.exit(0);
		}
		StringBuffer number = new StringBuffer();
		number.append(String.valueOf(divide));
		final int SIZE_OF_RESULT_ARRAYS = String.valueOf(+divide / +divider).length();
		int[] whole = new int[SIZE_OF_RESULT_ARRAYS];
		int[] balance = new int[SIZE_OF_RESULT_ARRAYS];
		int delta = 1;
		int index = 0;
		isDivideMinus(number);
		divider = isDividerMinus(divider);
		divide = Integer.valueOf(number.toString());
		int parse = divide;
		loop:while(divide - divider >= 0){
			if(number.length() > 0){
			parse = Integer.valueOf(number.substring(0,delta));
			}else{
				break loop;
			}
			while(parse < divider){
				if(delta <= number.length() - 1){
					delta++;
					continue loop;
				}else{
					break loop;
				}
			}
			addResult(divider, whole, balance, index, parse);
			index++;
			replace(divider, number, parse);
			delta = 1;
		}
		return packResult(complex.divide, complex.divider, whole, balance);
	}
	
	/**
	 * 
	 * @param divider
	 * @return int 
	 * method checks the divider to a negative value
	 */
	public static int isDividerMinus(int divider) {
		StringBuffer div = new StringBuffer();
		div.append(String.valueOf(divider));
		if(div.codePointAt(0) == '-'){	
			divider = Integer.valueOf(div.replace(0, 1, "").toString());
		}
		return divider;
	}
	
	/**
	 * 
	 * @param number
	 * method checks the divide to a negative value
	 */
	public static void isDivideMinus(StringBuffer number) {
		if(number.codePointAt(0) == '-'){
			number.replace(0, 1, "");
		}
	}

	/**
	 * 
	 * @param divider
	 * @param whole
	 * @param balance
	 * @param index
	 * @param parse
	 * method adds the result to an array
	 */
	public static void addResult(int divider, int[] whole, int[] balance,int index, int parse) {
		whole[index] = parse;
		balance[index] = parse / divider * divider;
	}

	/**
	 * 
	 * @param divide
	 * @param divider
	 * @param whole
	 * @param balance
	 * @return Result
	 * method initiates and fills in the Result
	 */
	public static Result packResult(int divide, int divider, int[] whole,
			int[] balance) {
		Result result = new Result();
		result.whole = whole;
		result.balance = balance;
		result.divide = divide;
		result.divider = divider;
		return result;
	}
	
	/**
	 * 
	 * @param divider
	 * @param number
	 * @param parse
	 * method shifts the value of the dividend in the fission
	 */
	public static void replace(int divider, StringBuffer number, int parse) {
		int balance;
		balance = parse % divider;
		if(balance == 0){
			number.replace(0,String.valueOf(parse).length(),"");
		}else{
			number.replace(0,String.valueOf(parse).length(), String.valueOf(balance));
		}
	}
	
	/**
	 * 
	 * @param result
	 * method displays the result on the screen
	 */
	public static void print(Result result){
		System.out.println("розрахунок");
		System.out.println(result.divide+" | "+result.divider);
		int indent ;
		int space = 0;
		if(result.divide < 0 ^ result.divider < 0){
			System.out.print(" ");
			space++;
		}
		System.out.print(result.balance[0]);
		printBell(String.valueOf(result.divide).length() - String.valueOf(result.balance[0]).length());
		System.out.println(" | "+result.divide/result.divider);
		printSeparator(String.valueOf(result.balance[0]).length());
		for(int i = 1 ; i < result.whole.length ; i++){
			indent = String.valueOf(result.whole[i-1] - result.balance[i-1]).length();
			space += String.valueOf(result.whole[i-1]).length() - indent ;
			printBell(space);
			System.out.println(result.whole[i]);
			printBell(space);
			System.out.println(result.balance[i]);
			printBell(space);
			printSeparator(String.valueOf(result.balance[i]).length());
		}
		indent = String.valueOf(result.whole[result.whole.length - 1] - result.balance[result.whole.length - 1]).length();
		space += String.valueOf(result.whole[result.whole.length - 1]).length() - indent ;
		printBell(space);
		System.out.println(result.divide % result.divider);
	}
	
	/**
	 * 
	 * @param size
	 * method prints to the screen space "size" number of times
	 */
	public static void printBell(int size){
		for(int i = 0 ; i < size ; i++){
			System.out.print(" ");
		}
	}
	
	/**
	 * 
	 * @param size
	 * method prints to the screen separator "size" number of times
	 */
	public static void printSeparator(int size){
		for(int i = 0 ; i < size ; i++){
			System.out.print("-");
		}
		System.out.println();
	}
}

class Complex{
	int divide;
	int divider;
}

class Result{
	int divide;
	int divider;
	int[] whole;
	int[] balance;
}


